#pragma once

class IRefCounted {
public:
	class Referencer {
		friend class IRefCounted;

	private:
		Referencer(IRefCounted* r) : _ref(r) {
			_ref->Ref();
		}

		IRefCounted* _ref;

	public:
		Referencer(const Referencer& rhs) : _ref(rhs._ref) {
			_ref->Ref();
		}

		const Referencer& operator=(const Referencer& rhs) {
			_ref->Unref();
			_ref = rhs._ref;
			_ref->Ref();
		}

		~Referencer() {
			_ref->Unref();
		}
	};

	IRefCounted();
	virtual ~IRefCounted();

	Referencer CreateRef() {
		return Referencer(this);
	}

	bool HasRef() { return m_Ref > 0; }

private:
	void Ref() {
		m_Ref++;
	}

	void Unref() {
		m_Ref--;
	}

	int m_Ref;
};
